<?php
function db_connect(){
    $use_dbver='5.0';
    $db_char='utf8';
    $db_dbname='log';
    $link=@mysql_connect('db-log-ro.cdn35.com:33064','redis','feeyo');
    if(!$link){
        echo"Cann't connect to DB!";
        exit();
    }
    //编码
    if($use_dbver>='4.1'){
        $q='';
        if($db_char){
            $q='character_set_connection='.$db_char.',character_set_results='.$db_char.',character_set_client=binary';
        }
        if($use_dbver>='5.0'){
            $q.=(empty($q)?'':',').'sql_mode=\'\'';
        }
        if($q){
            @mysql_query('SET '.$q);
        }
    }
    @mysql_select_db($db_dbname);
    mysql_query("set wait_timeout=2880000") or die(mysql_error());
    mysql_query("set interactive_timeout=24*3600*30") or die(mysql_error());
    return $link;
}

$sql="select
 result.id
,result.FlightNum3
,result.FlightNum2
,result.Registration
,result.DepCode
,ArrCode
,result.Lat
,result.Lng
,result.Speed
,result.Altitude
from
(
select
 a.id
,a.FlightNum3
,a.FlightNum2
,a.Registration
,a.DepCode,ArrCode
,a.Lat
,a.Lng
,a.Speed
,a.Altitude
,@v_rownum := @v_rownum+1
,if(@V_Registration=a.Registration,@v_rank:=@v_rank+1,@v_rank:=1) as 'rank_num'
,@V_Registration:=a.Registration
from
(
select
id,
FlightNum3,
FlightNum2,
Registration,
DepCode,ArrCode,
Lat,
Lng,
Speed,
Altitude
from `fa_airnav_".date('Ymd')."`
where DataSource='Veryzhun_KWL' and `Lat`<>0 and `Lng`<>0 and Registration <>''
order by Registration, id desc
)a,
(
select @v_rank:=0,@v_rownum:=0,@V_Registration:=null
) b
) result
where result.rank_num=1";
$link = db_connect();
$result = mysql_query ($sql, $link);

$result_array = array();
while(($tmp = mysql_fetch_assoc($result)) !== false) {
	
	$temp = array();
	$temp['flight'] = $tmp['Registration'];
	$temp['x'] = $tmp['Lng'];
	$temp['y'] = $tmp['Lat'];
	$temp['speed'] = $tmp['Speed'];
	$temp['height'] = $tmp['Altitude'];
	$result_array[$tmp['Registration']] = $temp;
}

// 给js准备数据
$result_array = json_encode($result_array);
?>


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=d279tZEmgEjv5ZgQrQLVjGvH"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/CurveLine/1.5/src/CurveLine.min.js"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js"></script>
	<script type="text/javascript" src="js/jquery.js"></script>
    <title>航线图</title>
    <style type="text/css">

		body,html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
		#first_container{width:100%;height:100%;}
		#second_container{width:100%;height:100%;}
		#third_container{width:100%;height:100%;}
		#fourth_container{width:100%;height:100%;}
		#r-result{width:160px;margin-top:5px;position:absolute;top:40%;left:10px}
		#r-height{width:160px;margin-top:5px;position:absolute;top:40%;right:10px}
		.view_control,.height_control{
			padding:6px 0 6px 0;
			background-color:#5c75a9;
			border-top:solid #8a9cc2 1px;
			text-align:center;
			color:#ffffff;
			cursor:pointer;
		}
		
		div.view_control:active,div.height_control:active{
			background-color:#4f6aa3;
		}
		div.BMap_scaleTxt{
			color:white !important;
		}
		
		/*除去百度logo旁边的字符*/
		.BMap_cpyCtrl{display: none;} 
    </style>
</head>
<body>
<div id="first_container"></div>

<div id="r-result">
	<div class="height_control" onclick="height_control(0);">所有飞机</div>
	<div class="height_control" onclick="height_control(6000);">高度低于6千米飞机</div>
</div>
	<script type="text/javascript">
		window.air_line = <?php echo $result_array;?>;
		window.height_set = 0;

		function height_control(height){
			window.height_set = height;
			paint_airline(first_map,air_line);
		}
		function get_source(){
			$.ajax({
				url:"source_kwl.php",
				type:"post", 
				timeout:300,
				async:false,
				dataType:"json",
				success:function(result){
					window.air_line = result;
				}
			});
		}

		setInterval("get_source()", 50000); 
		setInterval("paint_airline(first_map,air_line)", 60000); 
	
	style_json = [
			          {
		                    "featureType": "all",
		                    "elementType": "all",
		                    "stylers": {
		                              "hue": "#000000",
		                              "color":"#000000",
		                              "visibility": "off"
		                    }
			          }
				];
	
	// 飞机的显示逻辑
	 air_opts = {
	  width : 0,           // 信息窗口宽度
	  height: 0,               // 信息窗口高度
	  title : "飞行信息" ,  // 信息窗口标题
	  enableMessage:false //取消手机图片
	}
	
	window.air_marker = [];
	window.air_infoWindow = [];

	function paint_airline(map,point_array){
		
		for(exists in air_marker[map.id]) {
				(function(k){
					map.removeOverlay(air_marker[map.id][exists]); 
				})(map,exists)
			
		}
		air_marker[map.id] = [];
		for(key in point_array){
			if (height_set != 0) {
				if (point_array[key].height > height_set) {
					continue;
				}
			}
			var point = new BMap.Point(point_array[key].x,point_array[key].y);
			var myIcon = new BMap.Icon("./images/air.png", new BMap.Size(15,15));
			
			air_marker[map.id][key] = new BMap.Marker(point,{icon:myIcon});
			air_marker[map.id][key].b = true;
			map.addOverlay(air_marker[map.id][key]);  
			
			var txt = '航班号：'+ point_array[key].flight+'<br />飞行高度：'+point_array[key].height+'米<br />飞行速度：'+ point_array[key].speed+'公里/小时';
			air_infoWindow[key] = new BMap.InfoWindow(txt, air_opts);  // 创建信息窗口对象
			air_marker[map.id][key].addEventListener("mouseover", (function(k){
				return function(){
					air_marker[map.id][k].openInfoWindow(air_infoWindow[k]);
				}
			})(key));
		}
	}

	//var first_control = new BMap.ScaleControl({anchor:BMAP_ANCHOR_TOP_LEFT});

	first_map = new BMap.Map("first_container");
	first_map.id = 'first';
	paint_airline(first_map,air_line);// 画飞机
	// first_map.setMapStyle({styleJson:style_json});
	var first_point = new BMap.Point(110.28, 25.29); // 创建点坐标（桂林）
	//first_map.addControl(first_control);     // 添加比例尺
	first_map.centerAndZoom(first_point,10);// 初始化地图,设置中心点坐标和地图级别。
	first_map.enableScrollWheelZoom(); //启用滚轮放大缩小

	/*
	for(var i = 10 ;i>=1;i--) {
		var circle = new BMap.Circle(new BMap.Point(119.7197, 32.5633),(10000*i),{fillColor:"", strokeWeight: 1 ,fillOpacity: 1, strokeOpacity:0.4,strokeColor:'red'});
		first_map.addOverlay(circle);
	}
	*/
	var first_point_array = {
	
	};

	//paint_action(first_map,first_point_array);
</script>

</body>
</html>
